open GL
open Glu
open Glut

let rec show data = match data with
  | Types.Data.Int    i -> (string_of_int i) 
  | Types.Data.Float  x -> string_of_float x
  | Types.Data.String s -> s
  | Types.Data.List   l -> List.fold_left (fun acc elem -> acc ^ " " ^ show elem) "" l

let bar (x, y) (length, width) color maxvalue value =
  let value = Types.Data.to_float value in
  let drawSqr len =
    glVertex2 x y;
    glVertex2 (x+.len) y;
    glVertex2 (x+.len) (y+.width);
    glVertex2 x (y+.width) in
  glBegin GL_LINE_LOOP;
  glColor4v color;
  drawSqr length;
  glEnd ();
  glBegin GL_QUADS;
  glColor4v color;
  drawSqr (value/.maxvalue*.length);
  glEnd ()
  
let label (x, y) color font text =
  glColor4v color;
  glRasterPos2 x y;
  String.iter 
    (fun x -> glutBitmapCharacter font x) (show text)
  
let table (x, y) (length, width) bgColor textColor evenRowColor font rows =
  let curPos = ref 0.9 and curItem = ref 0 in
  let printRows row = 
    let curCol = ref 0.1 in
    let printCols row col =
      label (!curCol, row) textColor font col;
      curCol := !curCol+.0.3 in    
    let () = if !curItem mod 2 = 0 then 
	begin 
	  glBegin GL_QUADS;
	  glColor4v evenRowColor ;
	  (*shouldn't be hardcoded*)
	  glVertex2 x (!curPos-.0.01); 
	  glVertex2 x (!curPos+.0.025);
	  glVertex2 (x+.length) (!curPos+.0.025);
	  glVertex2 (x+.length) (!curPos-.0.01);
	  glEnd ();
	end in
    glColor4 1.0 1.0 1.0 1.0;
    List.iter (printCols !curPos) row;
    curPos := !curPos -. 0.04;
    curItem := !curItem+1; in
  glBegin GL_QUADS;
  glColor4v bgColor;
  glVertex2 x y;
  glVertex2 (x+.length) y;
  glVertex2 (x+.length) (y+.width);
  glVertex2 x (y+.width);
  glEnd ();
  List.iter printRows rows

let menu bgColor selColor textColor font items selected =
  let curPos = ref 0.9 and curItem = ref 0 in
  let printRows x = 
    let () = if !curItem = selected then 
	begin 
	  glBegin GL_QUADS;
	  glColor4v selColor ;
	  (*shouldn't be hardcoded*)
	  glVertex2 (1.0) (!curPos-.0.01); 
	  glVertex2 (1.0) (!curPos+.0.025);
	  glVertex2 (0.0) (!curPos+.0.025);
	  glVertex2 (0.0) (!curPos-.0.01);
	  glEnd ();
	end in
    label (0.1, !curPos) textColor font (x); 
    curPos := !curPos -. 0.04;
    curItem := !curItem+1; in
  glBegin GL_QUADS;
  glColor4v bgColor;
  glVertex2 (1.0) (1.0);
  glVertex2 (1.0) (0.0);
  glVertex2 (0.0) (0.0);
  glVertex2 (0.0) (1.0);
  glEnd ();
  List.iter printRows items
